﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VSDBM.Code.dto;
using System.Data;
using MySql.Data.MySqlClient;

/// <summary>
/// Summary description for Reference_Feature
/// </summary>


namespace VSDBM.Code.dao
{
    public class Reference_FeatureDAO
    {

        public static String add(Reference_Feature feature)
        {
            try
            {
                MySqlConnection connection = Connection.getConnection();


                MySqlCommand query = new MySqlCommand("INSERT INTO reference_features (description, positioninitial, positionfinal, idseqref, name, featurekey) VALUES ('" + feature.description + "'," + feature.positionInitial + "," + feature.positionFinal + "," + feature.idSeqRef + ",'" + feature.name +"','" + feature.featurekey + "')", connection);
                query.ExecuteNonQuery();
                return "Added";
            }
            catch (Exception e)
            {
                return "ERRO! - " + e.Message;
            }
        }

        public static void delete(Reference_Feature feature)
        {
            try
            {
                MySqlConnection connection = Connection.getConnection();
                MySqlCommand query = new MySqlCommand("DELETE FROM reference_features WHERE id = " + feature.id + "", connection);
                query.ExecuteNonQuery();
                return;
            }
            catch (Exception e)
            {
                throw e;

            }
        }

        public static DataSet select(String filters, String orderby)
        {

            DataSet dSet = new DataSet();
            try
            {
                MySqlConnection connection = Connection.getConnection();
                MySqlDataAdapter adapter = new MySqlDataAdapter();
                adapter.SelectCommand = new MySqlCommand("SELECT * FROM reference_features " + filters + " " + orderby, connection);
                adapter.Fill(dSet);
                return dSet;
            }
            catch (Exception e)
            {
                throw e;

            }
        }

        public static Reference_Feature Fill(int id, String description, int positioninitial, int positionfinal, int idseqref, String name, String featurekey)
        {
            Reference_Feature feature = new Reference_Feature(id,description, positioninitial, positionfinal, idseqref, name, featurekey);
            return feature;
        }

        public static List<Reference_Feature> DStoList(DataSet ds)
        {
            List<Reference_Feature> list = new List<Reference_Feature>();
            try
            {
                for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
                {
                    DataTable registro = ds.Tables[0];
                    list.Add(Fill(int.Parse(registro.Rows[i]["id"].ToString()), registro.Rows[i]["description"].ToString(), int.Parse(registro.Rows[i]["positioninitial"].ToString()), int.Parse(registro.Rows[i]["positionfinal"].ToString()), int.Parse(registro.Rows[i]["idseqref"].ToString()), registro.Rows[i]["name"].ToString(), registro.Rows[i]["featurekey"].ToString()));
                }

            }
            catch (Exception e)
            {
                throw e;
            }
            return list;
        }

    }
}